<?php

namespace app\admin\controller\yungift;

use app\common\controller\Backend;
use \app\admin\model\yungift\Group as GroupModel;

/**
 * 分组模块
 */
class Group extends Backend
{
    /**
     * @var GroupModel
     */
    protected $model;

    /**
     * 无需鉴权的方法(需登录)
     * @var array
     */
    protected $noNeedRight = ['groupList'];

    /**
     * 快捷搜索的字段
     * @var string
     */
    protected $searchFields = 'id, name';

    protected $groupList = [];
    protected $groupMap = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new GroupModel();

        $this->groupList = GroupModel::getTreeList();
        $groupMap = ['' => '无'];
        foreach ($this->groupList as $item) {
            $groupMap[$item['id']] = $item['name'];
        }
        $this->groupMap = $groupMap;
        $this->view->assign('groupMap', $this->groupMap);
        $this->view->assign('statusList', $this->model::getStatusList());
        $this->assignconfig('statusList', $this->model::getStatusList());
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $list = $this->groupList;
            $total = count($list);
            $result = array('total' => $total, 'rows' => $list);

            return json($result);
        }

        return $this->view->fetch();
    }
}
